package com.alipay.sdk.util;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.Point;
import android.hardware.display.DisplayManager;
import android.os.Build;
import android.text.TextUtils;
import android.view.Display;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;

/**
 * @ProjectName: 团餐
 * @ClassName: DeviceUtil
 * @Description: 设备相关
 * @Author: bd-001935
 * @CreateDate: 4/13/22 2:23 PM
 * @UpdateUser: bd-001935
 * @UpdateDate: 4/13/22 2:23 PM
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
public class DeviceUtil {

    private static final String UNKNOWN = "unknown";

    public static JSONObject getDeviceInfo(Context context) {
        JSONArray screenInfos = new JSONArray();
        DisplayManager displayManager = (DisplayManager) context.getSystemService(Context.DISPLAY_SERVICE);
        Display[] presentationDisplays = displayManager.getDisplays();
        for (Display display : presentationDisplays) {
            JSONObject object = new JSONObject();
            object.put("name", display.getName());
            object.put("displayId", display.getDisplayId());
            object.put("width", display.getWidth());
            object.put("height", display.getHeight());
            screenInfos.add(object);
        }

        JSONObject object = new JSONObject();
        object.put("sn", getDeviceSerialNumber());
        object.put("os_version", getOS());
        object.put("model", getModel());
        object.put("brand", getBrand());
        object.put("RomVersionName", getRomVersionName());
        object.put("RomVersionCode", getRomVersionCode());
        object.put("screenInfos", screenInfos);
        object.put("appVersionName", getAppVersionName(context));
        object.put("appVersionCode", getAppVersionCode(context));
        object.put("applicationId", getProcessName(context));
        object.put("appName", getAppName(context));
        return object;
    }


    /**
     * 获取设备序列号SN
     *
     * @return
     */
    public static String getDeviceSerialNumber() {
//        if (BuildConfig.BUILD_TYPE.equals("debug")) {
//            String deviceId = getProperties("ro.serialno", "");
//            if (TextUtils.isEmpty(deviceId))
//                return "DA4321AV40047";
//        }
        return getProperties("ro.serialno", "");
    }

    /**
     * 获取系统版本
     *
     * @return
     */
    public static String getSystemVersion() {
        return getProperties("ro.alipayiot.build.version", "");
    }

    private static String getProperties(String name, String def) {
        try {
            Class<?> SystemProperties = Class.forName("android.os.SystemProperties");
            Method get = SystemProperties.getDeclaredMethod("get", String.class, String.class);
            return (String) get.invoke(null, name, def);
        } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
            LogUtil.printStackTrace(e);
            return def;
        }
    }

    public static String getOS() {
        return Build.VERSION.RELEASE == null ? UNKNOWN : Build.VERSION.RELEASE;
    }

    public static String getModel() {
        return TextUtils.isEmpty(Build.MODEL) ? UNKNOWN : Build.MODEL.trim();
    }

    /**
     * 获取smile app版本号
     *
     * @param context
     * @return
     */
    public static String getSmileAPPVersion(Context context) {
        try {
            PackageInfo packageInfo = context
                    .getPackageManager().getPackageInfo("com.alipay.zoloz.smile",
                            PackageManager.GET_CONFIGURATIONS);
            if (packageInfo != null) {
                return packageInfo.versionName;
            }
            return "";
        } catch (PackageManager.NameNotFoundException e) {
            LogUtil.printStackTrace(e);
            return "";
        }
    }

    public static String getBrand() {
        return getProperties("ro.product.brand", "");
    }

    public static String getRomVersionName() {
        return getProperties("ro.version.sunmi_versionname", "");
    }

    public static String getRomVersionCode() {
        return getProperties("ro.version.sunmi_versioncode", "");
    }

    /**
     * 获取屏幕高度
     *
     * @param display
     * @return
     */
    public static int getScreenHeight(Display display) {
        if (display == null)
            return 0;
        Point size = new Point();
        display.getSize(size);
        return size.y;
    }

    /**
     * 获取App版本号
     *
     * @param context
     * @return
     */
    public static String getAppVersionName(Context context) {
        if (context == null)
            return "";
        try {
            PackageManager packageManager = context.getPackageManager();
            PackageInfo packageInfo = packageManager.getPackageInfo(context.getPackageName(), 0);
            return packageInfo.versionName;
        } catch (Exception e) {
            LogUtil.printStackTrace(e);
        }
        return "";
    }

    public static String getAppVersionCode(Context context) {
        if (context == null)
            return "";
        try {
            PackageManager packageManager = context.getPackageManager();
            PackageInfo packageInfo = packageManager.getPackageInfo(context.getPackageName(), 0);
            return packageInfo.versionCode + "";
        } catch (Exception e) {
            LogUtil.printStackTrace(e);
        }
        return "";
    }

    public static String getProcessName(Context context) {
        if (context == null)
            return "";
        try {
            return context.getApplicationInfo().processName;
        } catch (Exception e) {
            LogUtil.printStackTrace(e);
        }
        return "";
    }

    /**
     * 获取应用名称
     *
     * @param context Context
     * @return 应用名称
     */
    public static CharSequence getAppName(Context context) {
        if (context == null) return "";
        try {
            PackageManager packageManager = context.getPackageManager();
            ApplicationInfo appInfo = packageManager.getApplicationInfo(context.getPackageName(),
                    PackageManager.GET_META_DATA);
            return appInfo.loadLabel(packageManager);
        } catch (Exception e) {
            LogUtil.printStackTrace(e);
        }
        return "";
    }

}
